<template>
	<el-dialog :title="titleMap[mode]" v-model="visible"  :width="950"  destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="150px" label-position="left">
			<el-form-item label="区域编码" prop="area_code">
				<el-input v-model="form.area_code" clearable></el-input>
			</el-form-item>
			<el-form-item label="区域名称" prop="area_name">
				<el-input v-model="form.area_name" clearable></el-input>
			</el-form-item>
			<el-form-item label="简拼" prop="simple_spelling">
				<el-input v-model="form.simple_spelling" clearable></el-input>
			</el-form-item>
			<el-form-item label="排序" prop="sort_code">
				<el-input v-model="form.sort_code" clearable></el-input>
			</el-form-item>
			<el-form-item label="是否启用" prop="status">
				<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="备注">
				<el-input v-model="form.remark" clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增项',
				edit: '编辑项'
			},
			visible: false,
			isSaveing: false,
			form: {},
			rules: {
				area_code: [
					{ required: true, message: '请输入区域编码' }
				],
				area_name: [
					{ required: true, message: '请输入区域名称' }
				],
				simple_spelling: [
					{ required: true, message: '请输入简拼' }
				],
				sort_code: [
					{ required: true, message: '请输入排序' }
				]
			},
			dic: [],
		}
	},

	mounted() {
		if (this.params) {
			this.form.parent_id = this.params.parent_id
		}
	},

	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},

		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					this.$API['sys.area'].store.post(this.form)
						.then((res) => {
							if (res.code == 0) {
								this.$emit('success', this.form, this.mode)
								this.visible = false;
								this.$message.success("操作成功")
							} else {
								this.$alert(res.msg, "提示", { type: 'error' })
							}
						}).catch((msg) => {
							this.$alert(msg, "提示", { type: 'error' })
						}).finally(() => {
							this.isSaveing = false;
						});
				}
			})
		},

		//表单注入数据
		setData(data) {
			this.form = data
			this.form.parent_id = data.area.area_id
		}
	}
}
</script>

<style></style>
